<?php
/**
 * Created by PhpStrom
 * User: 郭碧云
 * Date: 2021/4/1 10:09
 * Desc:
 */

namespace App\Models\Admin;


use App\Models\BaseModel;

class Org extends BaseModel
{
    protected $table = "org";

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s',
        'updated_at' => 'datetime:Y-m-d H:i:s'
    ];

    protected $fillable=["name","parent_id","children_num","depth","sort","description","code","site_id","creator"];

    /**
     * 组织机构列表
     * @param $filed 查询的字段，默认查询全部，全部参数 "*",筛选字段为[$filed1,$filed2,$filed3,...]
     * @param $where 条件数组
     * @param $isSite 条件数组 是否是站点管理员，默认为否
     * @param $orgs 条件数组 用户可管理的部门id数组
     */
    public function getList($filed="*",$where=[],$isSite="",$orgs=[]) {
        //结果
        if(getCurrentSiteId() == 0 || !empty($isSite)){
            return self::whereBuilder($where)->orderBy("sort", "asc")->get($filed)->toArray();
        }
        return self::whereBuilder($where)->whereIn("id",$orgs)->orderBy("sort", "asc")->get($filed)->toArray();
    }

    /**
     * 组织机构详情
     * @param $id 查询的编号
     */
    public function getInfo($id){
        return getCurrentSiteId()==0?self::withoutGlobalScope('site_id_scope')->find($id)->toArray():self::find($id)->toArray();
//        return self::withoutGlobalScope('site_id_scope')->find($id)->toArray();
    }

    /**
     * 排重
     */
    public function repeate($params){
        return $params["parent_id"]==0?self::where("parent_id",0)->where("name",$params["name"])->get()->toArray():self::where("parent_id",$params["parent_id"])->where("name",$params["name"])->get()->toArray();
    }
}
